package unipi.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import unipi.database.User;
import unipi.helpers.HTMLPages;
import unipi.helpers.Hash;
import unipi.helpers.LoginCookie;
import unipi.helpers.StatChartHandler;
import unipi.helpers.Validator;

public class Statistics extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	public Statistics()
	{
		super();
	}
	
	public void init() throws ServletException
	{
		
	
	}	
	
	private String getPage(HttpServletRequest request, String uid, String userID)
	{
		String content = "<h1>Στατιστικά</h1>";
		if(uid != userID)
		{
			Map<String, String> data = User.getUserData(userID);
			if(data != null)
				content += "<h2>του χρήστη " + data.get("surname") + " " + data.get("name") + "</h2>";			
		}
		
		String pcpieFileName = "";
		String tpmbarFileName = "";
		String relpieFileName = "";
		String ppmbarFileName = "";
		String ppdpieFileName = "";
		String uidHash = Hash.SHA1(userID);
		
		try
		{
			pcpieFileName = StatChartHandler.getLatePendingPieChart(userID, uidHash, request);
			relpieFileName = StatChartHandler.getLateCompletedPieChart(userID, uidHash, request);
			tpmbarFileName = StatChartHandler.getTaskPerMonthBarChart(userID, uidHash, request);
			ppmbarFileName = StatChartHandler.getProgressPerMonthLineChart(userID, uidHash, request);
			ppdpieFileName = StatChartHandler.getProgressPerDayBarChart(userID, uidHash, request);
		}
		catch(IOException io)
		{
			System.out.println("IOException on Chart creation : " + io.toString());
		}
		
		content += "<table><tr>";
		if(tpmbarFileName != null) content += "<td><img src=\"" + tpmbarFileName + "\" alt=\"Chart\" /></td>";		
		if(ppmbarFileName != null) content += "<td><img src=\"" + ppmbarFileName + "\" alt=\"Chart\" /></td>";		
		content += "</tr><tr>";
		if(ppdpieFileName != null) content += "<td colspan=\"2\"><img src=\"" + ppdpieFileName + "\" alt=\"Chart\" /></td>";	
		content += "</tr><tr>";
		if(pcpieFileName != null) content += "<td><img src=\"" + pcpieFileName + "\" alt=\"Chart\" /></td>";	
		if(relpieFileName != null) content += "<td><img src=\"" + relpieFileName + "\" alt=\"Chart\" /></td>";	
		content += "</tr></table>";
				
		return HTMLPages.getGeneralPage(content, uid);	
	}
		
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		if(!LoginCookie.authCookie(request))
			response.sendRedirect("Login");
		else
		{
			String uid = LoginCookie.Get(request)[0];
		
			request.setCharacterEncoding("utf-8");
			String userID = request.getParameter("id");
			if(userID == null) userID = uid;
			if(!Validator.isNumber(userID)) userID = uid;
			
			response.setContentType("text/html; charset=UTF-8");	
			PrintWriter out = response.getWriter();
			
			out.println(getPage(request, uid, userID));
			
			out.close();			
		}
	}
	
}
